﻿using System.Collections.Generic;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.Site.BackEnd.ApCode.Filters;


namespace GTAuto.Site.BackEnd.Controllers
{
   [AutoSection]
    public class ModificationPropertyController : AdminBaseController
    {
        public ActionResult Index(int id)
        {
            ViewBag.CategoryName = DALPropertyCategory.Singleton.Get(id).Name;
            Session["CurCat"] = id;
            List<GTModificationProperty> gtModificationProperties = DALModificationProperty.Singleton.GetByCategory(id);
            return View(gtModificationProperties);
        }

        public ActionResult Edit(int? id)
        {

            if (Session["CurCat"] == null)
                return RedirectToAction("Index", "Home");

            var curCat = (int)Session["CurCat"];
            var cat = DALPropertyCategory.Singleton.Get(curCat);


            ViewBag.gtCategories = new SelectList(DALPropertyCategory.Singleton.GetAll((BrandTypeEnum)cat.Type), "Id", "Name");





            var gtSelects = new List<GTSelect> { new GTSelect { Id = "1", Name = "Текст" }, new GTSelect { Id = "2", Name = "Выбор" } };

            ViewBag.TypeSel = new SelectList(gtSelects, "Id", "Name");


            GTModificationProperty modificationProperty;
            if (id == null)
            {
                modificationProperty = new GTModificationProperty();
                modificationProperty.Category = curCat;
            }
            else
            {
                modificationProperty = DALModificationProperty.Singleton.Get((int)id);

                string selValues = "";

                foreach (var gtModificationPropertySelectValue in DALModificationPropertySelectValue.Singleton.GetByPropertyId((int)id))
                {
                    selValues = selValues + "|" + gtModificationPropertySelectValue.Value;
                }

                if (selValues.StartsWith("|"))
                {
                    selValues = selValues.Remove(0, 1);
                }

                ViewBag.SelValues = selValues;

            }

            return View(modificationProperty);
        }

        [HttpPost]
        public ActionResult Edit(GTModificationProperty idModificationProperty, FormCollection collection)
        {
            if (Session["CurCat"] == null)
                return RedirectToAction("Index", "Home");

            var curCat = (int)Session["CurCat"];
            var cat = DALPropertyCategory.Singleton.Get(curCat);


            var gtSelects = new List<GTSelect> { new GTSelect { Id = "1", Name = "Текст" }, new GTSelect { Id = "2", Name = "Выбор" } };

            ViewBag.TypeSel = new SelectList(gtSelects, "Id", "Name");


            ViewBag.gtCategories = new SelectList(DALPropertyCategory.Singleton.GetAll((BrandTypeEnum)cat.Type), "Id", "Name");

            var Error = new List<string>();

            if (string.IsNullOrEmpty(idModificationProperty.Name))
                Error.Add("Название не может быть пустым");

            ViewBag.Errs = Error;


            ViewBag.SelValues = collection["SelValues"];

            if (idModificationProperty.Type == 2 && string.IsNullOrEmpty(collection["SelValues"]))
            {
                Error.Add("Значения для выбора , при выборе типа значения 'Выбор' не может быть пустым");
            }


            if (Error.Count == 0)
            {
                string[] strings = collection["SelValues"].Split('|');

                if (idModificationProperty.Id == 0)
                {

                    idModificationProperty = DALModificationProperty.Singleton.Add(idModificationProperty);
                }
                else
                {
                    DALModificationProperty.Singleton.Update(idModificationProperty);
                }

                DALModificationPropertySelectValue.Singleton.Delete(idModificationProperty.Id);

                if (idModificationProperty.Type == 2)
                    foreach (var s in strings)
                    {
                        var newIt = new GTModificationPropertySelectValue();
                        newIt.Id = idModificationProperty.Id;
                        newIt.Value = s;
                        DALModificationPropertySelectValue.Singleton.Add(newIt);
                    }


                return RedirectToAction("Index", new { @id = idModificationProperty.Category });
            }

            return View(idModificationProperty);
        }

        public ActionResult Delete(int id)
        {
            var modificationProperty = DALModificationProperty.Singleton.Get((int)id);
            int catID = modificationProperty.Category;
            DALModificationProperty.Singleton.Delete(id);
            return RedirectToAction("Index", new { @id = catID });
        }


    }
}
